# coding: utf8

import tensorflow as tf


class CNN:

    @staticmethod
    def conv2d(x, **kwargs):
        W = CNN.weight_variable([kwargs["ksize"], kwargs["ksize"], kwargs["channel"], kwargs["knum"]])
        b = CNN.bias_variable([kwargs["knum"]])
        return tf.nn.relu(
            tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME") + b)

    @staticmethod
    def max_pool_2x2(x):
        return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                              padding="SAME")

    @staticmethod
    def weight_variable(shape, stddev=0.1):
        initial = tf.truncated_normal(shape, stddev=stddev)
        return tf.Variable(initial)

    @staticmethod
    def bias_variable(shape, init_value=0.1):
        initial = tf.constant(init_value, shape=shape)
        return tf.Variable(initial)

    @staticmethod
    def flat(x, xdim):
        return tf.reshape(x, [-1, xdim])

    @staticmethod
    def full_connection(x, **kwargs):
        W = CNN.weight_variable([kwargs["xdim"], kwargs["ydim"]])
        b = CNN.bias_variable([kwargs["ydim"]])
        return tf.nn.relu(tf.matmul(x, W) + b)

    @staticmethod
    def softmax(x):
        return tf.nn.softmax(x)

    @staticmethod
    def dropout(x, keep_prob):
        return tf.nn.dropout(x, keep_prob)
